//
//  CMGetQuestionParser.m
//  CreditManger
//
//  Created by chaiyanan on 15/11/9.
//  Copyright © 2015年 jinxin. All rights reserved.
//

#import "CMGetQuestionParser.h"
#import "CMGetQuestionInfo.h"
#import "CMGetQuestionItemInfo.h"
#import "CMGetQuestionAnswerInfo.h"

@implementation CMGetQuestionParser

-(ParserResult*)parseResponseDictionary:(NSDictionary *)responseDictionary
{
    LLog(@"responseDictionary:%@",responseDictionary);
    ParserResult* result=[[ParserResult alloc]init];
    NSNumber* codeObject=[responseDictionary objectForKey:Key_Code];
    if (codeObject)
    {
        NSInteger code = [codeObject integerValue];
        result.code = code;
        result.message = [responseDictionary objectForKey:Key_Msg];
        if (code == 0)
        {
            NSArray *dataAry = [responseDictionary objectForKey:Key_Data];
            if (dataAry&&[dataAry isKindOfClass:[NSArray class]])
            {
                CMGetQuestionInfo *info = [[CMGetQuestionInfo alloc]init];
                NSMutableArray *dataMulAry = [NSMutableArray arrayWithCapacity:dataAry.count];
                for (int i=0;i<dataAry.count;i++)
                {
                    NSDictionary *dict = dataAry[i];
                    
                    if ([dict isKindOfClass:[NSDictionary class]])
                    {
                        CMGetQuestionItemInfo *itemInfo = [[CMGetQuestionItemInfo alloc]init];
                        
//添加了一个默认的选项卡状态-----------------------------------------------------------------------------
                        if (i == 0) {
                            itemInfo.questionStatus = QuestionStatus_Open;
                        }else{
                            itemInfo.questionStatus = QuestionStatus_Close;
                        }
                        
//---------------------------------------------------------------------------------------------------
                        
                        if (dict.allKeys.count>0)
                        {
                            for (NSString *dictKey in dict.allKeys)
                            {
                                if ([dictKey hasPrefix:@"answer"])
                                {
                                    NSArray *answerAry = dict[dictKey];
                                    if (answerAry&&[answerAry isKindOfClass:[NSArray class]])
                                    {
                                        NSMutableArray *answerMulAry = [NSMutableArray arrayWithCapacity:answerAry.count];
                                        for (NSDictionary *answerDict in answerAry)
                                        {
                                            if (answerDict&&[answerDict isKindOfClass:[NSDictionary class]])
                                            {
                                                CMGetQuestionAnswerInfo *answerInfo = [[CMGetQuestionAnswerInfo alloc]init];
                                                if (answerDict.allKeys.count>0)
                                                {
                                                    answerInfo.answerKey = answerDict.allKeys[0];
                                                    answerInfo.answerContent = answerDict[answerDict.allKeys[0]];
                                                }
                                                [answerMulAry addObject:answerInfo];
                                                
                                            }
                                            else
                                            {
                                                result.code=1;
                                                result.message=@"服务器数据异常";
                                                return result;
                                            }
                                        }
                                        itemInfo.answers = answerMulAry;
                                    }
                                    else
                                    {
                                        result.code=1;
                                        result.message=@"服务器数据异常";
                                        return result;
                                    }
                                }
                                else
                                {
                                    itemInfo.questionKey = dictKey;
                                    itemInfo.questionContent = dict[dictKey];
                                }
                            }
                            
                            
                        }
                        [dataMulAry addObject:itemInfo];
                        
                    }
                    else
                    {
                        result.code=1;
                        result.message=@"服务器数据异常";
                        return result;
                    }
                }
                info.dataAry = dataMulAry;
                result.result = info;
            }
            else
            {
                result.code=1;
                result.message=@"服务器数据异常";
            }
        
        }
        
    }
    else
    {
        result.code=1;
        result.message=@"服务器数据异常";
    }
    
    return result;
}


@end
